<?php

/**
 * This is an authentication backend that uses DAV Principals to manage passwords.
 * 
   * Copyright © 2010 Jack Cleaver
   * 
   * This plugin provides functionality added by CalDAV (RFC 4791)
   * It implements new reports, and the MKCALENDAR method.
   *
   * This file is part of Slash.
   *
   * Slash is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * Slash is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with Slash.  If not, see <http://www.gnu.org/licenses/>.
 */
class Slash_DAV_Auth_Backend_BasicDAV 
	extends Sabre_DAV_Auth_Backend_AbstractBasic 
{

    /**
     * List of users 
     * 
     * @var array
     */
    protected $princCollection;

    /**
     * Creates the backend object. 
     *
     * If the filename argument is passed in, it will parse out the specified file fist.
     * 
     * @param string $filename 
     * @return void
     */
    public function __construct(Slash_Config $config) 
    {
    	$server = Slash_CalDAV_Server::getInstance();
    	$principalRoot = $server->getPrincipalRoot();
    	$this->princCollection = $server->tree->getNodeForPath($principalRoot);    	
    	//Slash_Debug::debug("Slash_BasicDAV: princCollection uri=" . $this->princCollection->getUri());
    }

    
    public function validateUserPass($username, $password)
    {
    	Slash_Debug::debug("Slash_BasicDAV validateUserPass: user=$username password=$password");
    	$result = false;
    	try
    	{
    		$principal = $this->princCollection->getChild($username);
    		$propreq[] = "{http://jackpot.uk.net/slash}password";
    		$properties = $principal->getProperties($propreq);
    		if (isset($properties["{http://jackpot.uk.net/slash}password"]))
    		{
    			$princpass = $properties["{http://jackpot.uk.net/slash}password"];
    			Slash_Debug::debug("BasicDAV_validateUserPass: password=$princpass, user supplied $password");
    			if ($princpass == $password)
    			{	
						$uriNode = new Slash_ACL_FSExt_UriNode($principal);    				
      			$result = array("password"=>null, "uri"=>$uriNode->getUri());
    				//$result = $username;    		
    			}
    		} else {
    			//No password set; let him in.
    			//TODO: devise a policy for this.
					$uriNode = new Slash_ACL_FSExt_UriNode($principal);    				
      		$result = array("password"=>null, "uri"=>$uriNode->getUri());
    		}
    	}
    	catch (Sabre_DAV_Exception_FileNotFound $e)
    	{
    		throw new Sabre_DAV_Exception_NotAuthenticated("Could not find principal resource");
    	}
    	Slash_Debug::debug("Slash_BasicDAV validateUserPass: outcome=" . ($result?"OK":"Failed"));
    	return $result;
    }

    /**
     * Returns a users' information
     * 
     * @param string $realm 
     * @param string $username 
     * @return string 
     */
    public function getUserInfo($realm, $username) 
    {
    	$principal = null;
    	try
    	{
    		$principal = $this->princCollection->getChild($username);
    	}
    	catch (Sabre_DAV_Exception $e)
    	{
    		return false;
    	}
			$uriNode = new Slash_ACL_FSExt_UriNode($principal);    				
    	return $uriNode->getUri();
    }


    /**
     * Returns the full list of users.
     *
     * This method must at least return a uri for each user.
     * 
     * @return array 
     */
    public function getUsers() 
    {
        $users = array();
        $principals = $this->princCollection->getChildren(); 
        foreach($principals as $principal) 
        {
        	$uriNode = new Slash_ACL_FSExt_UriNode($principal);
          $users[] = array('uri'=>$uriNode->getUri());
        }
        return $users;
    }

    
}
